﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RIMS.Lib.Models;
using RIMS.Lib.Manager;
using System.Reflection;
using RIMS.Web.Utility;
using RIMS.Web.Utility.CacheUtility;

namespace RIMS.Web.Controllers
{
    public class RoleController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Role role, string leon)
        {

            IList<Role> roles = RoleCacheUtility.All;
            int count = RoleCacheUtility.GetCount();
            int count2 = UserCacheUtility.GetCount();
            IList<User> users = UserCacheUtility.All;
            
            role.OID = Guid.NewGuid();
            role.Version = role.Version + 1;
            role.CreatedBy = new Guid( "5dfa745e-9608-438e-90b8-2cbb7e317695");
            role.CreationDate = DateTime.Now;
            role.ModifiedBy = new Guid("5dfa745e-9608-438e-90b8-2cbb7e317695");
            role.ModificationDate= DateTime.Now;

            bool b = new RoleManager().Save(role);

            RoleCacheUtility.Add(role);


            return View("Index");

        }

        //public ActionResult List()
        //{
        //    IList<Role> roleList = new RoleManager().GetAll();

        //    FlexiGridObject flexiGridObject = new FlexiGridObject();
        //    flexiGridObject.page = 1;
        //    flexiGridObject.total = roleList.Count();
        //    foreach (Role item in roleList)
        //    {
        //        FlexiGridRow row = new FlexiGridRow()
        //        {
        //            id = item.OID.ToString(),
        //            cell = {item.OID.ToString(), item.Name, item.Description }
        //        };
        //        flexiGridObject.rows.Add(row);
        //    }

        //    return View("Index", flexiGridObject);

        //}

        //public JsonResult List()
        //{
        //    JsonResult json = new JsonResult();


        //    IList<Role> roleList = new RoleManager().GetAll();

        //    json.Data = new
        //    {
        //        page = 1,
        //        total = roleList.Count(),
        //        rows = roleList.ToList().Select(x =>
        //                new
        //                {
        //                    id = x.OID.ToString(),
        //                    cell = new string[] { x.OID.ToString(), x.Name, x.Description }
        //                })
        //    };


        //    return json;
        //}

        public ActionResult List()
        {
            IList<Role> roleList = new RoleManager().GetAll();


            FlexiGridObject flexiGridObject = new FlexiGridObject();
            flexiGridObject.page = 1;
            flexiGridObject.total = roleList.Count();
            foreach (Role item in roleList)
            {
                FlexiGridRow row = new FlexiGridRow()
                {
                    id = item.OID.ToString(),
                    cell = { item.OID.ToString(), item.Name, item.Description }
                };
                flexiGridObject.rows.Add(row);
            }


            return Json(flexiGridObject);
        }


        //public JsonResult List()
        //{
        //    IList<Role> roleList = new RoleManager().GetAll();


        //    FlexiGridObject flexiGridObject = new FlexiGridObject();
        //    FlexiGridRow row = new FlexiGridRow();

        //    flexiGridObject.page = 1;
        //    flexiGridObject.total = roleList.Count();
        //   row = roleList.ToList().Select(x =>
        //                new
        //                {
        //                    id= x.OID.ToString(),
        //                    cell = new string[]{ x.OID.ToString(), x.Name, x.Description }
        //                })
        //};


        //    return Json(flexiGridObject);
        //} 


        private List<string> GetPropertyList(object obj)
        {
            List<string> propertyList = new List<string>();

            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                object o = property.GetValue(obj, null);
                propertyList.Add(o == null ? "" : o.ToString());
            }

            return propertyList;
        }
    }
}
